package lew.bing.novels;

import nl.siegmann.epublib.domain.*;
import nl.siegmann.epublib.service.MediatypeService;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collections;

/**
 * Created by 刘国兵 on 2019/8/19.
 *
 * @author 刘国兵
 */
public class EpubWriter implements NovelWriter {

    Book book;

    private String name;
    private String parent;


    private static String template = "<?xml version=\"1.0\" encoding=\"$charset\" ?>\n" +
            "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n" +
            "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"zh-CN\">" +
            "<head>" +
            "<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=$charset\" />" +
            "<title>$title</title></head>"+
            "<body>" +
            "<h1>$title</h1>"+
            "<div>$content</div>"+
            "</body>"+
            "</html>";

    private static String sectionTemplate = "<p>$section</p>";

    public EpubWriter(String name, String author) {
        this(name, author, "./");
    }

    public EpubWriter(String name, String author,  String parent) {
        this.name = name;
        this.parent = parent;
        book = new Book();

        Metadata metadata = book.getMetadata();
        metadata.setAuthors(Collections.singletonList(new Author(author)));
        metadata.setTitles(Collections.singletonList(name));
        metadata.setLanguage("zh-CN");
    }

    @Override
    public void write(Novel novel) throws IOException {
        if(novel == null) {
            return;
        }
        String idx = String.valueOf(novel.getIdx() + 1);
        TOCReference section = book.addSection(novel.getTitle(), new Resource(idx,
                content(novel),
                "chapter" + idx + ".html",
                MediatypeService.XHTML));
        section.setTitle(novel.getTitle());
    }

    private byte[] content(Novel novel) throws IOException{
        String novelContent = novel.getContent();
        String[] split = novelContent.split("(\r)?\n");
        StringBuilder buffer = new StringBuilder();
        for (String s : split) {
            if (s != null && !"".equals(s.trim())) {
                buffer.append(sectionTemplate.replace("$section", "&nbsp;&nbsp;"+s.trim()));
            }
        }
        Charset charset = novel.getCharset();
        String content = template.replace("$title", novel.getTitle()).replace("$content", buffer.toString()).replace("$charset", charset.name());
        return content.getBytes(charset);
    }

    @Override
    public void close() throws IOException {
        nl.siegmann.epublib.epub.EpubWriter epubWriter = new nl.siegmann.epublib.epub.EpubWriter();
        File file = new File(parent + name + ".epub");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        epubWriter.write(book, new FileOutputStream(file));
    }
}
